package com.dycc.sitemanager.view;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dycc.libary.config.MainLooperManage;
import com.dycc.sitemanager.R;
import com.dycc.sitemanager.R2;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 欢迎页面
 * Created by wxk on 2018/1/22 0022.
 */

public class WelcomActivity extends Activity {

    @BindView(R2.id.welcome_show_imageview)
    ImageView welcomeShowImageview;
    @BindView(R2.id.welcome_skip_button)
    Button welcomeSkipButton;
    @BindView(R2.id.welcome_bottom_rlayout)
    RelativeLayout welcomeBottomRlayout;

    @BindView(R.id.activity_welcom)
    LinearLayout activityWelcom;
    Timer timer;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        ButterKnife.bind(this);
        initView();
        initData();
    }


    public void initView() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
        Glide.with(this).load(R.drawable.test)
                .asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(welcomeShowImageview);
    }

    public void initData() {
//        timer = new Timer(5 * 1000, 1000, this);
//        timer.start();
    }

    public void toMain() {
        MainLooperManage.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ARouter.getInstance().build("/main_componet/activity/MainActivity")
                        .greenChannel().navigation();//跳过所有拦截器
                finish();
            }
        });
    }

    public void refreshWaitCount(final long count) {
        MainLooperManage.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                welcomeSkipButton.setText("跳过(" + (count / 1000) + "s)");
            }
        });
    }


    @OnClick({R.id.welcome_show_imageview, R.id.welcome_skip_button, R.id.welcome_bottom_rlayout, R.id.activity_welcom})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.welcome_show_imageview:
                break;
            case R.id.welcome_skip_button:
                try {
                    timer.cancel();
                } catch (Exception ex) {
                }
                toMain();
                break;
            case R.id.welcome_bottom_rlayout:

                break;
            case R.id.activity_welcom:
                break;
        }
    }


    public static class Timer extends CountDownTimer {
        public WeakReference<WelcomActivity> weakReference;

        public Timer(long millisInFuture, long countDownInterval, WelcomActivity activity) {
            super(millisInFuture, countDownInterval);
            weakReference = new WeakReference<WelcomActivity>(activity);
        }

        @Override
        public void onTick(long l) {//固定间隔时间调用
            weakReference.get().refreshWaitCount(l);
        }

        @Override
        public void onFinish() {//计时结束
            weakReference.get().toMain();
        }
    }
}
